package busi

import (
    "fmt"
    "log"
    "time"

    "github.com/dtm-labs/dtm/dtmcli"
    "github.com/gin-gonic/gin"
)

// 事务参与者的服务地址
const qsBusiAPI = "/api/busi_start"
const qsBusiPort = 8082

var qsBusi = fmt.Sprintf("http://localhost:%d%s", qsBusiPort, qsBusiAPI)

// QsMain will be call from dtm/qs
func QsMain() {
    QsStartSvr()
    QsFireRequest()
    select {}
}

// QsStartSvr quick start: start server
func QsStartSvr() {
    app := gin.New()
    qsAddRoute(app)
    log.Printf("quick start examples listening at %d", qsBusiPort)
    go func() {
        _ = app.Run(fmt.Sprintf(":%d", qsBusiPort))
    }()
    time.Sleep(100 * time.Millisecond)
}

func qsAddRoute(app *gin.Engine) {
    app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) {
        log.Printf("TransIn")
        // c.JSON(200, "")
        c.JSON(409, "") // Status 409 for Failure. Won't be retried
    })
    app.POST(qsBusiAPI+"/TransInCompensate", func(c *gin.Context) {
        log.Printf("TransInCompensate")
        c.JSON(200, "")
    })
    app.POST(qsBusiAPI+"/TransOut", func(c *gin.Context) {
        log.Printf("TransOut")
        c.JSON(200, "")
    })
    app.POST(qsBusiAPI+"/TransOutCompensate", func(c *gin.Context) {
        log.Printf("TransOutCompensate")
        c.JSON(200, "")
    })
}

const dtmServer = "http://localhost:36789/api/dtmsvr"

// QsFireRequest quick start: fire request
func QsFireRequest() string {
    req := &gin.H{"amount": 30} // 微服务的载荷
    // DtmServer为DTM服务的地址
    saga := dtmcli.NewSaga(dtmServer, dtmcli.MustGenGid(dtmServer)).
        // 添加一个TransOut的子事务，正向操作为url: qsBusi+"/TransOut"， 逆向操作为url: qsBusi+"/TransOutCompensate"
        Add(qsBusi+"/TransOut", qsBusi+"/TransOutCompensate", req).
        // 添加一个TransIn的子事务，正向操作为url: qsBusi+"/TransOut"， 逆向操作为url: qsBusi+"/TransInCompensate"
        Add(qsBusi+"/TransIn", qsBusi+"/TransInCompensate", req)
    // 提交saga事务，dtm会完成所有的子事务/回滚所有的子事务
    err := saga.Submit()

    if err != nil {
        panic(err)
    }
    return saga.Gid
}
